package com.kayo.specimen.config;

import com.kayo.specimen.annotion.PagingParam;
import com.kayo.specimen.converter.DateFormatter;
import com.kayo.specimen.intercepter.CurrentUserInterceptor;
import com.kayo.specimen.resolver.CurrentUserArgumentResolver;
import com.kayo.specimen.resolver.JsonArgumentResolver;
import com.kayo.specimen.resolver.PagingParamResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private CurrentUserInterceptor currentUserInterceptor;

    @Autowired
    private CurrentUserArgumentResolver currentUserArgumentResolver;

    @Autowired
    private DateFormatter dateFormatter;

    @Autowired
    private JsonArgumentResolver jsonArgumentResolver;

    @Autowired
    private PagingParamResolver pagingParamResolver;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentUserArgumentResolver);
        argumentResolvers.add(jsonArgumentResolver);
        argumentResolvers.add(pagingParamResolver);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(currentUserInterceptor)
                .addPathPatterns("/**");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(dateFormatter);
    }

}
